(function($){
	//选择城市
	function SelectCity(options){
		this.opts = $.extend({top:0,provDataUrl:'',cityDataUrl:'',cityClick:function(){}},options);
		this.touchCapable = 'ontouchstart' in window || (window.DocumentTouch && document instanceof window.DocumentTouch);
		this.eventToCallbackMap = {};
		this._state = {
			enabled: false,
			offset: null,
			size: null,
			pageY: null
		};
		this._createEl();
		this._loadProvData();
		this._buildMouse();
	}
	
	$.extend(SelectCity.prototype, {
		_createEl:function(){
			//获取当前区域高度
			var height = (window.screen.height*0.8)-this.opts.top;
			var html = '<div class="jf-selectCity" style="display:none;top:'+this.opts.top+'px;">'
						+'<div class="jf-selectCity-back"></div>'
						+'<div class="content-panel">'
							+'<div class="row">'
								+'<div class="col-xs-5" style="height:'+height+'px;">'
									+'<div class="jf-selectCity-province">'
									+'<ul class="nav"></ul>'
									+'</div>'
								+'</div>'
								+'<div class="col-xs-7" style="height:'+height+'px;">'
									+'<div class="jf-selectCity-city">'
									+'<ul class="nav"></ul>'
									+'</div>'
								+'</div>'
							+'</div>'
						+'</div>'
					+'</div>';
			$('body').append(html);
			$('.jf-selectCity-back').click(function(){$('.jf-selectCity').hide();});
			return this;
		},
		_loadProvData:function(){
			var self=this;
			$.getJSON(this.opts.provDataUrl,function(result){
				if(result.error==0){
					for(var i=0;i<result.data.length;i++){
						var item=result.data[i];
						$('.jf-selectCity-province .nav').append('<li data-id="'+item.id+'" data-fullName="'+item.fullName+'">'+item.name+'</li>');
					}
					self._buildProvClick();
				}
			});
		},
		_buildProvClick:function(){
			var url=this.opts.cityDataUrl;
			var cityClick=this.opts.cityClick;
			$('.jf-selectCity-province .nav>li').click(function(){
				$('.jf-selectCity-province .nav>li.active').removeClass('active');
				var provId=$(this).addClass('active').data('id');
				$('.jf-selectCity-city .nav').empty();
				var provEl=$(this);
				$.getJSON(url,{provId:provId},function(result){
					if(result.error==0){
						for(var i=0;i<result.data.length;i++){
							var item=result.data[i];
							$('.jf-selectCity-city .nav').append('<li data-id="'+item.id+'" data-fullName="'+item.fullName+'">'+item.name+'</li>');
						}
						$('.jf-selectCity-city .nav>li').click(function(){
							cityClick.apply(this,provEl);
						});
					}
				});
			});
		},
		_offsetLeft: function(obj){
			return obj.getBoundingClientRect().left;
		},
		_offsetTop: function(obj){
			var offsetTop = obj.offsetTop;
			while((obj = obj.offsetParent) && !isNaN(obj.offsetTop)){
				offsetTop += obj.offsetTop;
			}
			return offsetTop;
		},
		_offset: function (obj) {
			return {
				left: this._offsetLeft(obj),
				top: this._offsetTop(obj)
			};
	    },
	    _pauseEvent: function(ev) {
			if(ev.stopPropagation) {
				ev.stopPropagation();
			}
		    if(ev.preventDefault) {
		    	ev.preventDefault();
		    }
		    ev.cancelBubble=true;
		    ev.returnValue=false;
		},
		_mousedown:function(ev){
			var el = ev.currentTarget;
			if (this.touchCapable && (ev.type === 'touchstart' || ev.type === 'touchmove')) {
				ev = ev.touches[0];
			}
			this._state.enabled=true;
			this._state.offset = this._offset(el);
			this._state.size = el['offsetHeight'];
			this._state.pageY = ev['pageY'];
			//console.log("_mousedown:" + this._state.pageY);
		},
		_mouseup:function(ev){
			this._state.enabled=false;
			//this._trigger(ev);
			//console.log('_mouseup');
		},
		_mousemove:function(ev){
			var el = ev.currentTarget;
			if(this._state.enabled){
				this._pauseEvent(ev);
			}else{return;}
			
			if (this.touchCapable && (ev.type === 'touchstart' || ev.type === 'touchmove')) {
				ev = ev.touches[0];
			}
			
			var eventPosition = ev['pageY'];
			if(Math.abs(eventPosition - this._state.pageY) > 10){
				var sliderOffset = this._state.offset['top'];
				var distanceToSlide = eventPosition - this._state.pageY + sliderOffset;
				if(this._state.enabled && distanceToSlide > 0){
					$(el).css('top',0);
				}else{
					$(el).css('top',distanceToSlide);
				}
				//console.log(distanceToSlide);
			}
		},
		_buildMouse:function(){
			var provEl = $('.jf-selectCity-province')[0];
			this.mousedown = this._mousedown.bind(this);
			this.mousemove = this._mousemove.bind(this);
			this.mouseup = this._mouseup.bind(this);
			if (this.touchCapable) {
				// Bind touch handlers
				provEl.addEventListener("touchstart",this.mousedown,false);
				provEl.addEventListener("touchmove",this.mousemove,false);
				provEl.addEventListener("touchend",this.mouseup,false);
			}
			provEl.addEventListener("mousedown",this.mousedown,false);
			provEl.addEventListener("mousemove",this.mousemove,false);
			provEl.addEventListener("mouseup",this.mouseup,false);
			
			var cityEl = $('.jf-selectCity-city')[0];
			if (this.touchCapable) {
				// Bind touch handlers
				cityEl.addEventListener("touchstart",this.mousedown,false);
				cityEl.addEventListener("touchmove",this.mousemove,false);
				cityEl.addEventListener("touchend",this.mouseup,false);
			}
			cityEl.addEventListener("mousedown",this.mousedown,false);
			cityEl.addEventListener("mousemove",this.mousemove,false);
			cityEl.addEventListener("mouseup",this.mouseup,false);
		},
		toggle:function(){
			$('.jf-selectCity').toggle();
		}
	});
	window.jfSelectCity = SelectCity;
})(window.jQuery);